﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.models;
using Newtonsoft.Json;
using george.Web;

// ReSharper disable once CheckNamespace
namespace com.nm.Helper {
	/// <summary>
	/// JSON相关操作
	/// </summary>
	public static class JsonHelper {
		/// <summary>
		/// JsonConvert.SerializeObject(obj)的快捷方式
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public static string ToJson(this object obj) {
			return JsonConvert.SerializeObject(obj);
		}
		/// <summary>
		/// JsonConvert.DeserializeObject&lt;T&gt;(str)的快捷方式
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="str"></param>
		/// <returns></returns>
		public static T FromJson<T>(this string str) {
			return JsonConvert.DeserializeObject<T>(str);
		}

		/// <summary>
		/// JsonConvert.DeserializeObject(str,type)的快捷方式
		/// </summary>
		/// <param name="str"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public static object FromJson(this string str, Type type) {
			return JsonConvert.DeserializeObject(str, type);
		}

        /// <summary>
        /// 返回公司数据源字典
        /// </summary>
        /// <returns></returns>
	    public static IList<CompanyIDExtrSource> CompanyIDExtrSourceDic()
	    {
	        var str = myCache.GetFile("CompanyIDExtrSourceDic", "~/assets/json/companyExtrSourceConfig.json");
	        return str.FromJson<IList<CompanyIDExtrSource>>();
	    }
    }
}
